package org.jeecg.modules.website.service.impl;

import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.website.entity.WebTradeNotice;
import org.jeecg.modules.website.entity.WebWorkFile;
import org.jeecg.modules.website.mapper.WebTradeNoticeMapper;
import org.jeecg.modules.website.mapper.WebWorkFileMapper;
import org.jeecg.modules.website.service.IWebTradeNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 交易信息公告表
 * @Author: jeecg-boot
 * @Date:   2024-07-17
 * @Version: V1.0
 */
@Service
public class WebTradeNoticeServiceImpl extends ServiceImpl<WebTradeNoticeMapper, WebTradeNotice> implements IWebTradeNoticeService {
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private WebWorkFileMapper webWorkFileMapper;
    @Autowired
    private WebTradeNoticeMapper webTradeNoticeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(WebTradeNotice webTradeNotice, List<WebWorkFile> webWorkFileList) {
        webTradeNoticeMapper.insert(webTradeNotice);
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webTradeNotice.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(WebTradeNotice webTradeNotice,List<WebWorkFile> webWorkFileList) {
        webTradeNoticeMapper.updateById(webTradeNotice);

        //1.先删除子表数据
        webWorkFileMapper.deleteByMainId(webTradeNotice.getId());

        //2.子表数据重新插入
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webTradeNotice.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        webWorkFileMapper.deleteByMainId(id);
        webTradeNoticeMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            webWorkFileMapper.deleteByMainId(id.toString());
            webTradeNoticeMapper.deleteById(id);
        }
    }
    public String getDictDescByCategoryAndType(String category, String type) {
        // 根据 category 确定字典编码
        // String dictCode = determineDictCodeByCategory(category);
        // 查询字典项
        String dictItem = sysDictService.queryDictTextByKey(category, type);
        return dictItem ;
    }
    /*
    public String determineDictCodeByCategory(String category) {
        int ts=Integer.parseInt(category);
        if (ts == 1) {
            return "sys_trade_type_jgcg";
        } else if (ts == 2) {
            return "sys_trade_type_zfcg";
        } else if (ts == 3) {
            return "sys_trade_type_tdkq";
        }
        else if (ts == 4) {
            return "sys_trade_type_cqjy";
        }
        else if (ts == 5) {
            return "sys_trade_type_other";
        }
        else if (ts == 6) {
            return "sys_trade_type_jqzc";
        } else if (ts == 7) {
            return "sys_trade_type_sm";
        }
        else if (ts == 8) {
            return "sys_trade_type_rzgg";
        }
        throw new IllegalArgumentException("Invalid category: " + category);
    }

     */
}
